package app.beit.finportal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import app.beit.finportal.bean.NewsBean;
import app.beit.finportal.service.NewsService;
import app.beit.finportal.utils.constr.SessionConstr;

@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {

	private final Logger log = Logger.getLogger(this.getClass());
	
	private final String PAGE = "news";
	
	@Autowired
	private NewsService newsService;
	
	@RequestMapping
	public String index(final HttpServletRequest request) throws Exception{
		super.startProcess(request);
		//super.removeAllSession(request);
		request.setAttribute(SessionConstr.ACTIVE_PAGE, PAGE);
		
		try{
			final List<NewsBean> beans = this.newsService.getAll();
			if(beans != null && beans.size() > 0){
//				final List<NewsBean> newsList = new ArrayList<NewsBean>();
//				for(final NewsBean bean : beans){
//					newsList.add(bean);
//				}
				request.setAttribute(SessionConstr.NEWS_ALL_DATAS, beans);
			}
		}catch(Exception ex){
			this.log.error(ex.getMessage());
			ex.printStackTrace();
		}
		
		return "news";
	}

	@RequestMapping("/{id}")
	public String details(final HttpServletRequest request, final @PathVariable("id") String id){
		//super.removeAllSession(request);
		request.setAttribute(SessionConstr.ACTIVE_PAGE, PAGE);
		
		try{
			super.startProcess(request);
			final NewsBean beans = this.newsService.getById(id);
			if(beans != null){
				request.setAttribute(SessionConstr.NEWS_DETAIL_DATA, beans);
			}
		}catch(Exception ex){
			this.log.error(ex.getMessage());
			ex.printStackTrace();
		}
		
		return "news.detail";
	}
	
}
